﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using GanagroLiteBO;

namespace GanagroLite
{
    public partial class FormReversos : WinPart
    {
        private GANADO_BO currentGanado;
        private Operacion_BO currentOperacion;

        #region Internal methods

        protected internal override object GetIdValue()
        {
            return "FormReversos";
        }

        public override string ToString()
        {
            return "Reversar operacion";
        }

        #endregion

        #region Constructors
        public FormReversos()
        {
            InitializeComponent();
        }
        #endregion

        private void textBoxCodigo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(textBoxCodigo.Text.Trim()))
                {
                    e.Cancel = true;
                }

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

        }

        private void textBoxCodigo_Validated(object sender, EventArgs e)
        {
            string errorMessage = string.Empty;
            this.labelActivo.ForeColor = Color.Transparent;

            currentGanado = new GanagroLiteBO.GANADO_BO(textBoxCodigo.Text);

            if (currentGanado.CodigoEsValido(ref errorMessage) == false)
            {
                this.errorProviderReversos.SetError(textBoxCodigo, errorMessage);
                this.btnGuardar.Enabled = false;
                return;
            }
            else
            {
                this.errorProviderReversos.SetError(textBoxCodigo, string.Empty);
            }

            this.btnGuardar.Enabled = true;
            this.bindingSourceMain.DataSource = currentGanado.ListOperaciones;
            this.dataGridView1.Enabled = true;
//          this.labelActivo.Visible = true;
            labelActivo.Text = (currentGanado.Activo>0 ? "Activo" : "Inactivo");
            labelActivo.ForeColor = (currentGanado.Activo > 0 ? Color.Black : Color.Red);

        }

        private void btnSalir_Click(object sender, EventArgs e)
        {
            this.bindingSourceMain.CancelEdit();
            this.Close();
        }

        private void bindingSourceMain_PositionChanged(object sender, EventArgs e)
        {
            bindingSourceMain.ResetBindings(false);
            if (bindingSourceMain.Count > 0 && bindingSourceMain.Position == 0)
            {
                currentOperacion = (Operacion_BO)bindingSourceMain.Current;
                btnGuardar.Enabled = true;
            }
            else
            {
                btnGuardar.Enabled = false;
            }

        }

        private void btnGuardar_Click(object sender, EventArgs e)
        {
            try
            {
                ReversoBO rBo ;

                if (currentOperacion == null)
                {
                    return;
                }

                string msg = string.Format("Esta seguro que quiere reversar esta {0} ?", utilities.GetDescOperacion((TiposOperacion)currentOperacion.OPERACION));

                if (MessageBox.Show(msg, "Confirmacion", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                {
                    return;
                }

                rBo = new ReversoBO(currentGanado, currentOperacion);
                rBo.reversar();

                if (currentOperacion.OPERACION != (short)TiposOperacion.compra)
                {
                    this.bindingSourceMain.DataSource = currentGanado.ListOperaciones;
                    this.bindingSourceMain.Sort = "FECHA DESC";
                    if (this.bindingSourceMain.Count > 0)
                    {
                        currentOperacion = (Operacion_BO)bindingSourceMain.Current;
                    }

                    labelActivo.ForeColor = (currentGanado.Activo > 0 ? Color.Black : Color.Red);
                }
                else
                {
                    msg = string.Format("La compra de [{0}] ha sido reversada",currentOperacion.CODIGO);
                    MessageBox.Show(msg, "Confirmacion", MessageBoxButtons.OK);
                    this.textBoxCodigo.Text = string.Empty;
                    this.bindingSourceMain.DataSource = new Operaciones();
                    labelActivo.ForeColor = Color.Transparent;
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void FormReversos_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
            this.textBoxCodigo.Focus();
            this.textBoxCodigo.Text = ".";
            this.textBoxCodigo.SelectAll();
        }

    }
}
